package com.ex.mzwqz.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ex.mzwqz.AppContext;
import com.ex.mzwqz.R;
import com.ex.mzwqz.model.NewsEntity;

import java.util.List;

/**新闻广告的adapter
 * Created by akira on 2016/4/26.
 */
public class AdPageAdapter extends PagerAdapter {


//    private String[] urls;

    private List<NewsEntity> adPics;
    private Context context;
    private LayoutInflater inflater;
    private OnItemAdListener onItemAdListener;
    private boolean isVideo;

    public AdPageAdapter(Context context, List<NewsEntity> adPics){
        this.adPics = adPics;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemAdListener(OnItemAdListener onItemAdListener){

        this.onItemAdListener = onItemAdListener;

    }



    @Override
    public int getCount() {

        return adPics.size()>0?adPics.size():0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
            View itemView = inflater.inflate(R.layout.item_ads,container,false);
            ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_pic);
            TextView  tvAdTitle = (TextView) itemView.findViewById(R.id.tv_pictext);
            tvAdTitle.setText(adPics.get(position).getTitle());
            ivPic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemAdListener!=null)
                        onItemAdListener.toAdDetail(position);
                }
            });
            AppContext.getInstance().getImageLoader().get(adPics.get(position).getImgUrl(),ivPic,context.getResources().getDrawable(R.drawable.img_news_default));
            container.addView(itemView);
            return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager)container).removeView((View) object);
    }

    public interface OnItemAdListener{

        void toAdDetail(int position);

    }

}
